package com.manong.bookstore.user.controller;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.manong.bookstore.user.entity.CollectEntity;
import com.manong.bookstore.user.service.CollectService;
import com.manong.common.utils.PageUtils;
import com.manong.common.utils.R;



/**
 * 
 *
 * @author cyq
 * @email 465447222@qq.com
 * @date 2021-01-27 21:34:32
 */
@RestController
@RequestMapping("user/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = collectService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{collectId}")
    public R info(@PathVariable("collectId") Integer collectId){
		CollectEntity collect = collectService.getById(collectId);

        return R.ok().put("collect", collect);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CollectEntity collect){
		collectService.save(collect);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CollectEntity collect){
		collectService.updateById(collect);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] collectIds){
		collectService.removeByIds(Arrays.asList(collectIds));

        return R.ok();
    }


    /**
     * 添加店铺收藏（检测重复版）
     */
    @RequestMapping("/save/collect")
    public R saveCollect(@RequestParam Map<String, Object> params){
        Object userId = params.get("userId");
        Object storeId = params.get("storeId");

        System.out.println("saveCollect: " + userId + " " + storeId);

        if(userId == null || storeId == null) {
            return R.ok().put("status", "error: miss paramter");
        }

        CollectEntity collect = collectService.queryByUserIdAndStoreId(userId.toString(), storeId.toString());

        if(collect != null)
            return R.ok().put("status", "ok").put("message", "repeat save");
        else {
            collect = new CollectEntity();
            collect.setUserId(new Integer(userId.toString()));
            collect.setStoreId(new Integer(storeId.toString()));

            collectService.save(collect);

            return R.ok().put("status", "ok").put("message", "success");
        }
    }

//    @RequestMapping("/findUserCollect")
//    public R findUserAddress(@RequestParam Integer userId) {
////        List<AddressEntity> result = null;
//        List<CollectEntity> result = collectService.list(new QueryWrapper<CollectEntity>().eq("user_id", userId));
//
//
//
//        return R.ok().put("collect", result);
//    }
}
